﻿
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("demo_test_post.asp",
    {
      name:"Donald Duck",
      city:"Duckburg"
    },
    function(data,status){
      alert("Data: " + data + "\nStatus: " + status);
    });
  });
});
function openURL( idst){
//alert(idst);
window.open ("view2.php?idst="+idst,"mywindow");

}
</script>



<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "aris";
mysql_connect($host, $user, $pass);
mysql_select_db($dbname);
mysql_query("SET NAMES UTF8");

$sql = "SELECT
student.id_st,
student.Pre_st,
student.name_st,
student.sname_st,
project.NPro,
project.bossproject,

result.idStu,
student.class_st,
result.dateStrat,
project.type
FROM
result
INNER JOIN student ON student.id = result.idStu
INNER JOIN project ON result.idPro = project.idPro
INNER JOIN machine ON machine.id = result.idTool
GROUP BY
result.dateStrat
ORDER BY
result.dateStrat DESC
LIMIT 0, 10
";
$rs = mysql_query($sql);
?>
<html>
<head>
	<title>วิธี SELECT ค่าข้อมูลจากฐานข้อมูลมาแสดงในหน้าจอ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link  href="table.css" rel="Stylesheet"/>

</head>
<body>
<center>
<a href="assa.php">หน้าแรก</a>
<table id="box-table-a" >
	<tr>
		
		
		<th align='left' width="180">ชื่อ - นามสกุล</th>
		<th align='center'>วิชา/หัวหน้าโครงการ</th>
		<th align='center'>วิชาเอก</th>
		<th align='center'>เครื่องที่ใช้</th>
		<th align='center'>เวลาใช้ล่าสุด</th>
	</tr>
	<?php
	while($row = mysql_fetch_array($rs)) {
		echo "<tr onclick=openURL('".$row['idStu']."')>" ."<td>" . $row['Pre_st'] ." ".$row['name_st']." ".$row['sname_st'] . "</td>" ; 
			
			if($row['type']==1)
				echo "<td align='center'>" . $row['NPro']." " .$row['bossproject'] . "</td>" ;
				else
				echo "<td align='center'>" ."โครงการ ". $row['NPro']."<br>หัวหน้าโครงการ " .$row['bossproject'] . "</td>" ;
		echo 	"<td align='center'>" . $row['class_st'] . "</td>" ;

 $rowMac=machine($row['idStu'],$row['dateStrat']);
 
		echo "<td align='center'>" ;

		foreach($rowMac as $value)
		echo $value ."<br>";
		//echo print_r($rowMac,true);
		echo "</td>";
		echo	"<td align='right'>" .substr($row['dateStrat'],11,8)."</br>".substr($row['dateStrat'],8,2)."-".substr($row['dateStrat'],5,2)."-".substr($row['dateStrat'],0,4)  . "</td>" .
		"</tr>";
	}
	?>
	
</table></center>
</body>
</html>
<?php 
function machine($idStu,$time){

$sql="
SELECT
machine.`name`
FROM
result
INNER JOIN machine ON machine.id = result.idTool
WHERE
result.idStu = '$idStu' AND
result.dateStrat = '$time'

";
$result =mysql_query($sql);
while($row = mysql_fetch_array($result))
    {
    	// I doubt you actually need to run stripslashes on your data...
    	//$row = array_map('stripslashes', $row);//
    	$row = $row['name'];//
    	$push[] = $row;
    }

    return $push;
}
?>